package com.c2b.algorithm.newcoder;

/**
 * <a href='https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337?tpId=295&tqId=23286&ru=%2Fexam%2Foj&qru=%2Fta%2Fformat-top101%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj'>BM4 合并两个排序的链表</a>
 *
 * @author c2b
 * @since 2024/3/14 10:14
 */
public class BM4Merge {

    public static class Solution {
        public ListNode Merge (ListNode pHead1, ListNode pHead2) {
            if (pHead1 == null) {
                return pHead2;
            }
            if (pHead2 == null) {
                return pHead1;
            }
            ListNode dummyNode = new ListNode(-1);
            ListNode currNode = dummyNode;
            while (pHead1 != null && pHead2 != null) {
                if (pHead1.val < pHead2.val) {
                    currNode.next = pHead1;
                    pHead1 = pHead1.next;
                } else {
                    currNode.next = pHead2;
                    pHead2 = pHead2.next;
                }
                currNode = currNode.next;
            }
            if (pHead1 != null) {
                currNode.next = pHead1;
            }
            if (pHead2 != null) {
                currNode.next = pHead2;
            }
            return dummyNode.next;
        }
    }
}
